<?php

namespace App\Http\Controllers;

use App\Models\Contact\Contact as ContactModel;
use App\Models\Contact\Type as TypeModel;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    // 首页
    public function index(Request $request)
    {

        $LoginUser = !empty($request->cookie('LoginUser')) ? json_decode($request->cookie('LoginUser'),true) : [];

        $userid = !empty($LoginUser['id']) ? $LoginUser['id'] : 0;

        $typeid = request('typeid');

        $keyword = request('keyword');

        $where[] = ['userid',$userid];

        if($typeid)
        {
            $where[] = ['typeid',$typeid];
        }

        if($keyword)
        {
            $where[] = ['nickname','like',"%$keyword%"];
        }

        $ContactList = ContactModel::where($where)->orderBy('createtime','desc')->get();

        $TypeList = TypeModel::get();

        $data = compact([
            'TypeList',
            'ContactList',
            'typeid'
        ]);
        return view('home.index',$data);
    }
}
